package game;

import java.util.ArrayList;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import game.Actors.Actor;
import game.Actors.Enemy;
import game.Config.Config;
import game.Config.WaveConfig;
/**
 * This class represents a LevelManager for the game, handling 
 * the different levels.
 * @author Sebastian Isheden and Victor Beer
 *
 */
public class LevelManager {
	private static int eggsRemaining = 0;
	private static boolean finished = false;
	private static boolean waveCompleted = false;
	private static int currentLevel = 0;
	private static int currentWave = 0;
	private static ArrayList<Level> levels= new ArrayList<Level>();
	private static ArrayList<Wave> waves = new ArrayList<Wave>();
	/**
	 * Intializes the LevelManager, adding the different levels available.
	 */
	public static void initialize() throws SlickException{
		levels.add(new Level(Config.BACKGROUND_LEVEL));
		waves = WaveConfig.getWaves();
	}
	
	/**
	 * @return Returns the current level.
	 */ 
	public static Level getCurrentLevel() {
		return levels.get(currentLevel);
	}
	/**
	 * Draws the current level.
	 */
	public static void drawCurrentLevel(Graphics graphics){
		levels.get(currentLevel).draw();
	}
	/**
	 * Draws the wave status.
	 * @param graphics
	 */
	public static void drawWaveStatus(Graphics graphics){
		if(waveCompleted){
			graphics.drawString("Wave "+currentWave+" completed.", Config.SCREEN_WIDTH/2, Config.SCREEN_HEIGHT/2);
		} else if(finished){
			graphics.drawString("You have finished the game!", Config.SCREEN_WIDTH/2, Config.SCREEN_HEIGHT/2);
		}
	}
	/**
	 * Returns a random path from the current level.
	 */
	public static NodeGraph getRandomPath(){
		return levels.get(currentLevel).getRandomPath();
	}
	
	/**
	 * Updates the current wave
	 * @param delta The time in millisecounds since the last update.
	 */
	public static void updateCurrentWave(int delta){
		if(currentWave >= waves.size()){//The game is over.
			finished = true;
			waveCompleted = false;
			return;
		}
		if(waves.get(currentWave).isFinished()){
			if(Enemy.getEnemyList().isEmpty()){
				currentWave++;
				waveCompleted = true;
			}
		}else{
			waves.get(currentWave).update(delta);
			if(waveCompleted){
				if(!Enemy.getEnemyList().isEmpty()){
					waveCompleted = false;
				}
			}
			
		}
	}

	/**
	 * Sets the difficulty. 1 = One egg, 2 = Two eggs....
	 * @param difficulty The number of eggs.
	 */
	public static void setDifficulty(int difficulty) {
		eggsRemaining = difficulty;
	}

	/**
	 * @return True if there is no eggs remaining.
	 */
	public static boolean checkLoss() {
		return eggsRemaining <= 0;	
	}
	
	/**
	 * Removes one egg.
	 */
	public static void destroyEgg(){
		eggsRemaining--;
	}
	
	/**
	 * Resets the level the and the current wave.
	 */
	public static void resetLevel() {
		Actor.clearActors();
		currentWave = 0;
		waves = WaveConfig.getWaves();
	}
}
